(function(){
	$.namespace("com.yogi.wrappers.common");

	/**
	 * { "type":"catalog_anchor","catalog":"homepage" }
	 */
	com.yogi.wrappers.common.CatalogAnchorWrapper = function() {
		com.yogi.wrappers.common.CatalogAnchorWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.CatalogAnchorWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.CatalogAnchorWrapper.superclass.wrap(part,wrapperConfig,partConfig);		
			
			part.waitForDiv(function(partComp){
				partComp.css("cursor","hand");
				partComp.click(function() {
					window.contentPanel.loadCatalog(wrapperConfig.catalog);
				});	
			});
		}
	});
	/**
	 * {
	 	"type":"composite",
	 	"children":[{
	 	 	"asset":"ct.subct.subsubct",
	 	 	"wrapper":{
		 		"type":"download",
		 		"file":"/agilegarden/flash/somefile.pdf"
	 		}
	 	}]
	 }
	 */
	com.yogi.wrappers.common.CompositeWrapper = function() {
		com.yogi.wrappers.common.CompositeWrapper.superclass.constructor.call(this);
		/**
		 * id of sub map to flexpart object
		 */
		this.m_child_map = [];
	};
	Ext.extend(com.yogi.wrappers.common.CompositeWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.CompositeWrapper.superclass.wrap(part,wrapperConfig,partConfig);
			//alert("wrap com.yogi.wrappers.common.CompositeWrapper.");
			var _self = this;
			if(this.m_config.children != null && $.isArray(this.m_config.children)) {
				var child_configs = this.m_config.children;
				$.each(child_configs, function(i,  child_cfg) {
					_self.processFlexPart(child_cfg);
				});
			}
		},
		processFlexPart: function (config) {
			var _instance = this;
			var asset_id = config.asset;	
			//var asset_sprite = com.yogi.wrappers.common.CompositeWrapper.superclass.findAsset(asset_id);	 	
			com.yogi.wrappers.common.CompositeWrapper.superclass.findAsset(asset_id, function(asset_sprite) {
				var partConfig = { "WrapperParam" : config.wrapper  };
				var childPart = new com.yogi.core.FlexPart(partConfig);
				var ref_part = com.yogi.wrappers.common.CompositeWrapper.superclass.getFlexPart();
							
				childPart.initUI(asset_sprite, ref_part);
				_instance.m_child_map.push(childPart);
			});			
		}
	});	
	/*
	 {
	 	"type":"download",
	 	"file":"/agilegarden/flash/somefile.pdf",
	 	"target":"_blank"
	 }
	*/
	com.yogi.wrappers.common.DownloadWrapper = function() {
		com.yogi.wrappers.common.DownloadWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.DownloadWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.DownloadWrapper.superclass.wrap(part,wrapperConfig,partConfig);
			
			var partComp = part.getDIV();
			partComp.css("cursor","hand");
			partComp.click(function() {
				if(wrapperConfig.target == "_blank"){
					window.open(wrapperConfig.file);
				}else{
					window.location.replace(wrapperConfig.file);
				}
			});	
		}
	});
	/**
	    "type":"web_anchor",
	 	"address":"www.basto.com.cn",
	 	"target":"_blank"
	 */
	com.yogi.wrappers.common.WebAnchorWrapper = function() {
		com.yogi.wrappers.common.WebAnchorWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.WebAnchorWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.WebAnchorWrapper.superclass.wrap(part,wrapperConfig,partConfig);		
			
			part.waitForDiv(function(partComp){
				partComp.css("cursor","hand");
				partComp.click(function() {
					var target = wrapperConfig.target || "_blank";
					if(target == "_blank"){
						window.open(wrapperConfig.address);
					} else {
						window.location.replace( wrapperConfig.address );
					}
				});	
			});
		}
	});
	/**
	 * { "type":"dummy" }
	 */
	com.yogi.wrappers.common.DummyWrapper = function() {
		com.yogi.wrappers.common.DummyWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.DummyWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.DummyWrapper.superclass.wrap(part,wrapperConfig,partConfig);			
		}
	});
	/**	
	 * {
	 * 	"type":'front_movie',
	 * 	"skip":'skip'
	 * }
	 */
	com.yogi.wrappers.common.FrontMovieWrapper = function() {
		com.yogi.wrappers.common.FrontMovieWrapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.FrontMovieWrapper,com.yogi.core.PartWrapperBase,{	
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.FrontMovieWrapper.superclass.wrap(part,wrapperConfig,partConfig);
			
			var hypeDoc = part.getHypeDoc();
			if( hypeDoc ) {				
				var scene = hypeDoc.scenes[ 0 ];
				if( window.contentPanel.designSite ) {
					function SaveHypeActionConfig( ) {		
						var js_oid = "com.yogi.core.ContentPanel.loadHomePage";
						scene.onSceneAnimationCompleteAction = {
								type : hypeDoc.kActionRunJavascript,
								javascriptOid : js_oid
						};
						//skip button
						var skip_guid = hypeDoc.elements_map[ wrapperConfig.skip ];
						var skip_btn = scene.initialValues[ skip_guid ];
						skip_btn.ActionOnMouseClick = {
								type : hypeDoc.kActionRunJavascript,
								javascriptOid : js_oid
						};
						var rm = new com.yogi.core.net.YogiSmileRemoteMethod("AppCatalogSupport");
						rm.callMethod("SaveHypeSceneConfig", [ hypeDoc.url, Ext.encode(scene)], function(data){
							alert("scene saved for FrontMovieWrapper");
						});
					}
					if(! scene.onSceneAnimationCompleteAction || scene.onSceneAnimationCompleteAction.type == 0) {
						SaveHypeActionConfig();
					}
				} 
				$.extend(hypeDoc.javascriptMapping, {
					"com.yogi.core.ContentPanel.loadHomePage": "com_yogi_core_ContentPanel_loadHomePage"
				});
				$.extend(hypeDoc.Custom, {
					"com_yogi_core_ContentPanel_loadHomePage":  function(){
						window.contentPanel.loadHomePage();
					}
				});
			}
		}
	});
	
	/**
	 * {
	 		"type":"image_swapper",
	 		"map":[["btn0","mc_z0",{"type":"play","in_frame":[10,14],"out_frame":[15]}],["btn1","mc_z1"],["btn2","mc_z2"]],
	 		"display":{ "type":"fadein","time":15,"tran":"easeOutExpo" },
			"default_btn":"btn0",
	 		"btn_active_frame":2,
	 		"btn_deactive_frame":1
	  }
	 */
	com.yogi.wrappers.common.ImageSwapper = function() {
		com.yogi.wrappers.common.ImageSwapper.superclass.constructor.call(this);
	};
	Ext.extend(com.yogi.wrappers.common.ImageSwapper,com.yogi.core.PartWrapperBase,{	
		m_default_display_config:null,
		wrap: function(part,wrapperConfig,partConfig) {
			com.yogi.wrappers.common.ImageSwapper.superclass.wrap(part,wrapperConfig,partConfig);
			
			var _self = this;
			
			this.m_default_display_config = wrapperConfig.display;
			if(wrapperConfig.map != null){
				var map = wrapperConfig.map;
				var default_index = 0;
				if(wrapperConfig.default_btn) {
					$.each(map, function(i, mapItem){
						if(mapItem[0] == wrapperConfig.default_btn){
							default_index = i;
							return false;
						}
					});
				}
				$.each(map, function(i, mapItem){
					if(mapItem.length >= 2){
						var btn = part.getElement[mapItem[0]];
						if(btn != null) {
							btn.css("cursor","hand");
							btn.click( function(){
								//TODO do hide action simulate flash's gotoAndStop method
							} );									
						}						
						var img = part.getElement[mapItem[1]];
						if(img != null)	{	
							if(mapItem.length == 2){
								mapItem.push(_self.m_default_display_config);		//把默认值配置到第三项，后面就直接从第三项获取配置
							}						
							if(i  != default_index){							
							 	img.alpha = 0;
							}else{
								m_top_level = img.parent.getChildIndex(img);
								
								//TODO research about action equivalent to the "gotoAndStop" method of flash's MovieClip
//								if(wrapperConfig.btn_active_frame != null && btn is MovieClip){
//									(btn as MovieClip).gotoAndStop(m_config.btn_active_frame);
//								}
							}
						}
					}
				});				
			}			
			//this.addEventListener(SHOW_DONE,onImageShow);
			this.addEventListener(HIDE_DONE,onImageHide);
		}
	});
	
	
	
	var wrapperManager = window.contentPanel.getWrapperManager();
	wrapperManager.registerWrapperClass("catalog_anchor", com.yogi.wrappers.common.CatalogAnchorWrapper);
	wrapperManager.registerWrapperClass("composite", com.yogi.wrappers.common.CompositeWrapper);
	wrapperManager.registerWrapperClass("download", com.yogi.wrappers.common.DownloadWrapper);
	wrapperManager.registerWrapperClass("web_anchor", com.yogi.wrappers.common.WebAnchorWrapper);
	wrapperManager.registerWrapperClass("dummy", com.yogi.wrappers.common.DummyWrapper);
	wrapperManager.registerWrapperClass("front_movie", com.yogi.wrappers.common.FrontMovieWrapper);
	wrapperManager.registerWrapperClass("image_swapper", com.yogi.wrappers.common.ImageSwapper);
})();